#include "Volume.h"
#include <stdlib.h>
#include <stdio.h>

#define WHERESTR  "Error[file %s, line %d]: "
#define WHEREARG  __FILE__, __LINE__

/*
 * Create a Volume (dim[0],dim[1],dim[2]) dimenssion
 *
 */
int createVolume(volume_t * volume, hsize_t dims[3])
{
	int	i;
	int	j;

	volume->dim[0] = dims[0];
	volume->dim[1] = dims[1];
	volume->dim[2] = dims[2];

	volume->position = (float*) malloc(3*sizeof(float));

	if ( (volume->memory = malloc(dims[0]*dims[1]*dims[2]*sizeof(float))) == NULL)
	{
		fprintf(stderr, WHERESTR"Error allocating memory in",WHEREARG);
		return -1;
	}

	float *         aux     = volume->memory;
        if ((volume->data  = malloc(dims[0]*sizeof(float**)) ) == NULL){
		fprintf(stderr, WHERESTR"Error allocating memory in",WHEREARG);
                return -1;
	}

        for(i = 0; i < dims[0]; i++)
        {
                if ((volume->data[i] = malloc(dims[1] * sizeof(float *))) == NULL)
		{
			fprintf(stderr, WHERESTR"Error allocating memory in",WHEREARG);
	                return -1;
		}
                for(j = 0; j < dims[1]; j++)
                {
                        volume->data[i][j] = aux;
                        aux += dims[2];
                }
        }

	return 0;	
}

/*      
 * Set a volume's position
 *      
 */
int setPositionVolume(volume_t * volume, float pos[3])
{
	volume->position[0] = pos[0];
        volume->position[1] = pos[1];
        volume->position[2] = pos[2];

	return 0;
}
/*
 * Destroy a Volume
 *
 */
int destroyVolume(volume_t volume)
{
	int	i;

	free(volume.memory);
	free(volume.position);
        for(i = 0; i < volume.dim[0]; i++)       
                free(volume.data[i]);

	return 0;
}
